package cn.wolfcode.web.controller;


import cn.wolfcode.domain.CourseOrder;
import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.query.CourseOrderQueryObject;
import cn.wolfcode.query.CustomerQueryObject;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.ICourseOrderService;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ICustomerService customerService;

    @RequiresPermissions(value = {"courseOrder:list", "课程订单页面"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list (Model model, @ModelAttribute("qo") CourseOrderQueryObject qo) {

        PageInfo<CourseOrder> pageInfo = courseOrderService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        //课程名称下拉框数据
        List<SystemDictionaryItem> course = systemDictionaryItemService.selectByPrentSn("subject");
        model.addAttribute("course", course);

        //客户名称下拉框数据
        List<Customer> customers = customerService.listAll();
        model.addAttribute("customers", customers);


        return "courseOrder/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"courseOrder:delete", "课程订单删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete (Long id) {
        if (id != null) {
            courseOrderService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"courseOrder:saveOrUpdate", "课程订单新增/编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate (CourseOrder courseOrder) {
        if (courseOrder.getId() != null) {
            courseOrderService.update(courseOrder);
        } else {

            courseOrderService.save(courseOrder);
        }
        return new JsonResult();
    }
}
